/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.is.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.is.po.IsMonitoringPO;
import com.pig4cloud.pigx.is.entity.IsMonitoring;
import com.pig4cloud.pigx.is.mapper.IsMonitoringMapper;
import com.pig4cloud.pigx.is.service.IsMonitoringService;
import com.pig4cloud.pigx.is.utils.MyTypeUtils;
import com.pig4cloud.pigx.is.vo.IsMonitoringVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * 监考明细信息
 *
 * @author skyorange
 * @date 2020-11-06 20:09:33
 */
@Service
public class IsMonitoringServiceImpl extends ServiceImpl<IsMonitoringMapper, IsMonitoring> implements IsMonitoringService {

	@Autowired
	private IsMonitoringMapper isMonitoringMapper;

	@Override
	public Page<IsMonitoringVO> getPage(Page page, IsMonitoring isMonitoring) {
		Page p = isMonitoringMapper.selectPage(page, Wrappers.query(isMonitoring).orderByDesc("time"));
		List<IsMonitoring> records = p.getRecords();
		List<IsMonitoringVO> list = new ArrayList();
		for (IsMonitoring record : records) {
			IsMonitoringVO v = BeanUtil.toBean(record, IsMonitoringVO.class);
			String s = "data:image/png;base64,";
			try{
				s = s + MyTypeUtils.InputStreamToBase64(record.getScreenshot());
			}catch (IOException e){
				System.out.println(e);
			}
			v.setScreenshotBase64(s);
			list.add(v);
		}
		Page<IsMonitoringVO> newP = p.setRecords(list);
		return newP;
	}
}
